<?php
/********************************************************/
/* Module Name:  csv_report.php						    */
/* Description: search and retrieve the information from */
/*    database and automatic download the report to local drive	*/
/* Written by: Maria Cielo Carreno							*/
/********************************************************/

session_start();
//If your session isn't valid, it returns you to the login screen for protection
if(empty($_SESSION['myusername'])){
 header("location:main_login.php");
}
if(empty($_SESSION['seedval'])){
 header("location:main_login.php");
}
//echo $_SESSION['myusername'];
if($_GET['id'] != $_SESSION['seedval'])
{
	echo 'Security breakage!';
	$error = 1;
}

 function DateEnd()
 {
	$curr_month = date("m");
	$curr_day = date("d");
	$curr_year = date("Y");
	$month = array (1=>"Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec ");
	$day_today = range(1,31);
	$year = range(1900,2020);
	//show month selection
	$select = "<select name=\"endmonth\">\n";
	foreach ($month as $key => $val) {
		$select .= "\t<option val=\"".$key."\"";
		if ($key == $curr_month) {
			$select .= " selected=\"selected\">".$val."</option>\n";
		} else {
			$select .= ">".$val."</option>\n";
		}
	}
	$select .= "</select>";
	echo $select;
	//show day selection
	$select = "<select name=\"endday\">\n";
	foreach ($day_today as $key => $val) {
		$select .= "\t<option val=\"".$key."\"";
		if ($val == $curr_day) {
			$select .= " selected=\"selected\">".$val."</option>\n";
		} else {
			$select .= ">".$val."</option>\n";
		}
	}
	$select .= "</select>";
	echo $select;
	//show year selection
	$select = "<select name=\"endyear\">\n";
	foreach ($year as $key => $val) {
		$select .= "\t<option val=\"".$key."\"";
		if ($val == $curr_year) {
			$select .= " selected=\"selected\">".$val."</option>\n";
		} else {
			$select .= ">".$val."</option>\n";
		}
	}
	$select .= "</select>";
	echo $select;	
 }
 
  function DateStart()
 {
	$curr_month = date("m");
	$curr_day = date("d");
	$curr_year = date("Y");
	$month = array (1=>"Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec ");
	$day_today = range(1,31);
	$year = range(1900,2020);
	//show month selection
	$select = "<select name=\"startmonth\">\n";
	foreach ($month as $key => $val) {
		$select .= "\t<option val=\"".$key."\"";
		if ($key == $curr_month) {
			$select .= " selected=\"selected\">".$val."</option>\n";
		} else {
			$select .= ">".$val."</option>\n";
		}
	}
	$select .= "</select>";
	echo $select;
	//show day selection
	$select = "<select name=\"startday\">\n";
	foreach ($day_today as $key => $val) {
		$select .= "\t<option val=\"".$key."\"";
		if ($val == $curr_day) {
			$select .= " selected=\"selected\">".$val."</option>\n";
		} else {
			$select .= ">".$val."</option>\n";
		}
	}
	$select .= "</select>";
	echo $select;
	//show year selection
	$select = "<select name=\"startyear\">\n";
	foreach ($year as $key => $val) {
		$select .= "\t<option val=\"".$key."\"";
		if ($val == $curr_year) {
			$select .= " selected=\"selected\">".$val."</option>\n";
		} else {
			$select .= ">".$val."</option>\n";
		}
	}
	$select .= "</select>";
	echo $select;	
 }

 echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
 echo '<html> <head>';
 echo '<link href="default.css" rel="stylesheet" type="text/css" />';
 echo '<title>Export CSV</title>';
 echo '</head> <body>';
 echo '<div id="fulladmin">';
 echo "<div style ='width: 350px; margin: 180px auto 0;'> <table border='1'><tr><th>";
 echo "<center><h1>Generate CSV File Report</h1><br>";


  function ReportForm($error)
 {
  // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 echo "<table border='0' cellpadding='10'>";
 echo '<tr><td><form action="" method="post">';
 echo "<div align='left'>";
 echo '<table border="0">';
 echo '<tr><td><strong>Date Start: </strong> </td><td>';
 DateStart();
 echo '</td></tr>';
 echo "<tr><td><strong>Date End:   </strong> </td><td>";
 DateEnd();
 echo "</td></tr>";
 echo "<br><br><center>";
 echo "<tr><td></td><td>";
 echo '<input type="submit" name="submit" value="Generate">';
 echo '<input type="submit" name="cancel" value="Cancel">';
 echo '</td></tr>';
 echo  '</table></div>';
 echo '</form> </th></tr></table> ';
 echo "</th></tr></table></div>";
 echo '<div id="footer"><center>Return to main <a href="dashboard.php"><font color="red">Dashboard</font></a>, or you can <a href="log_out.php"><font color="red">Log Out</font></a></center></div>';
 echo '</body> </html>';
 
   }  //end render function

   
	if (isset($_POST['submit']))
	{ 
		//print_r($_POST);
		//validate date
		$datefrom = strtotime($_POST['startday']." ".$_POST['startmonth']." ".$_POST['startyear']);
		//echo $datefrom."\n\r";
		$dateto = strtotime($_POST['endday']." ".$_POST['endmonth']." ".$_POST['endyear']);
		//echo $dateto;
		if($datefrom < $dateto){
		    $dateStart = date ( 'Y-m-d',$datefrom); 
			$dateEnd = date ( 'Y-m-d',$dateto); 
			//echo $dateStart."\n\r".$dateEnd;
			//ExportCSVReportMediationPerf($dateStart,$dateEnd);
			header("Location: csv_download.php?ds=".$dateStart."&de=".$dateEnd);
		}
		elseif($datefrom == $dateto){
		 	$error = "Date Start should not be the same with Date End.";
			ReportForm($error);
		}
		else{
		//if ($datefrom > $dateto){
			$error = "Date Start should not be ahead of Date End";
			ReportForm($error);
		}
	}
	else{
		$error = '';
		if(isset($_POST['cancel']))
			header("Location: dashboard.php");
		else
		ReportForm($error);
	}

 ?>